<?php
@ini_set("display_errors","1");
@ini_set("display_startup_errors","1");

include("include/dbcommon.php");
include("classes/searchclause.php");

add_nocache_headers();

include("include/company_address_variables.php");

if(!@$_SESSION["UserID"])
{ 
	$_SESSION["MyURL"]=$_SERVER["SCRIPT_NAME"]."?".$_SERVER["QUERY_STRING"];
	header("Location: login.php?message=expired"); 
	return;
}
if(!CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Export"))
{
	echo "<p>".mlang_message("NO_PERMISSIONS")."<a href=\"login.php\">".mlang_message("BACK_TO_LOGIN")."</a></p>";
	return;
}

$all=postvalue("all");

$pageName = "print.php";

include('include/xtempl.php');
include('classes/runnerpage.php');
$xt = new Xtempl();

$id = postvalue("id") != "" ? postvalue("id") : 1;
//array of params for classes
$params = array("pageType" => PAGE_PRINT, "id" =>$id, "tName"=>$strTableName);
$params["xt"] = &$xt;
	
$pageObject = new RunnerPage($params);


// add onload event
$onLoadJsCode = GetTableData($pageObject->tName, ".jsOnloadPrint", '');
$pageObject->addOnLoadJsEvent($onLoadJsCode);

// add button events if exist
$pageObject->addButtonHandlers();



// Modify query: remove blob fields from fieldlist.
// Blob fields on a print page are shown using imager.php (for example).
// They don't need to be selected from DB in print.php itself.
if(!postvalue("pdf"))
	$gQuery->ReplaceFieldsWithDummies(GetBinaryFieldsIndices());

//	Before Process event
if($eventObj->exists("BeforeProcessPrint"))
	$eventObj->BeforeProcessPrint($conn);

$strWhereClause="";
$strHavingClause="";

$selected_recs=array();
if (@$_REQUEST["a"]!="") 
{
	
	$sWhere = "1=0";	
	
//	process selection
	if (@$_REQUEST["mdelete"])
	{
		foreach(@$_REQUEST["mdelete"] as $ind)
		{
			$keys=array();
			$keys["id"]=refine($_REQUEST["mdelete1"][mdeleteIndex($ind)]);
			$selected_recs[]=$keys;
		}
	}
	elseif(@$_REQUEST["selection"])
	{
		foreach(@$_REQUEST["selection"] as $keyblock)
		{
			$arr=explode("&",refine($keyblock));
			if(count($arr)<1)
				continue;
			$keys=array();
			$keys["id"]=urldecode($arr[0]);
			$selected_recs[]=$keys;
		}
	}

	foreach($selected_recs as $keys)
	{
		$sWhere = $sWhere . " or ";
		$sWhere.=KeyWhere($keys);
	}
	$strSQL = gSQLWhere($sWhere);
	$strWhereClause=$sWhere;
}
else
{
	$strWhereClause=@$_SESSION[$strTableName."_where"];
	$strHavingClause=@$_SESSION[$strTableName."_having"];
	$strSQL = gSQLWhere($strWhereClause, $strHavingClause);
}
if(postvalue("pdf"))
	$strWhereClause = @$_SESSION[$strTableName."_pdfwhere"];

$_SESSION[$strTableName."_pdfwhere"] = $strWhereClause;


$strOrderBy=$_SESSION[$strTableName."_order"];
if(!$strOrderBy)
	$strOrderBy=$gstrOrderBy;
$strSQL.=" ".trim($strOrderBy);

$strSQLbak = $strSQL;
if($eventObj->exists("BeforeQueryPrint"))
	$eventObj->BeforeQueryPrint($strSQL,$strWhereClause,$strOrderBy);

//	Rebuild SQL if needed

if($strSQL!=$strSQLbak)
{
//	changed $strSQL - old style	
	$numrows=GetRowCount($strSQL);
}
else
{
	$strSQL = gSQLWhere($strWhereClause, $strHavingClause);
	$strSQL.=" ".trim($strOrderBy);
	
	$rowcount=false;
	if($eventObj->exists("ListGetRowCount"))
	{
		$masterKeysReq=array();
		for($i = 0; $i < count($pageObject->detailKeysByM); $i ++)
			$masterKeysReq[]=$_SESSION[$strTableName."_masterkey".($i + 1)];
			$rowcount=$eventObj->ListGetRowCount($pageObject->searchClauseObj,$_SESSION[$strTableName."_mastertable"],$masterKeysReq,$selected_recs);
	}
	if($rowcount!==false)
		$numrows=$rowcount;
	else
	{
		$numrows = gSQLRowCount($strWhereClause, $strHavingClause);
	}
}

LogInfo($strSQL);

$mypage=(integer)$_SESSION[$strTableName."_pagenumber"];
if(!$mypage)
	$mypage=1;

//	page size
$PageSize=(integer)$_SESSION[$strTableName."_pagesize"];
if(!$PageSize)
	$PageSize = GetTableData($strTableName,".pageSize",0);

if($PageSize<0)
	$all = 1;	
	
$recno = 1;
$records = 0;	
$maxpages = 1;
$pageindex = 1;

if(!$all)
{	
	if($numrows)
	{
		$maxRecords = $numrows;
		$maxpages = ceil($maxRecords/$PageSize);
					
		if($mypage > $maxpages)
			$mypage = $maxpages;
		
		if($mypage < 1) 
			$mypage = 1;
		
		$maxrecs = $PageSize;
	}
	$listarray = false;
	if($eventObj->exists("ListQuery"))
		$listarray = $eventObj->ListQuery($pageObject->searchClauseObj,$_SESSION[$strTableName."_arrFieldForSort"],$_SESSION[$strTableName."_arrHowFieldSort"],$_SESSION[$strTableName."_mastertable"],$masterKeysReq,$selected_recs,$PageSize,$mypage);
	if($listarray!==false)
		$rs = $listarray;
	else
	{
			if($numrows)
		{
			$strSQL.=" limit ".(($mypage-1)*$PageSize).",".$PageSize;
		}
		$rs = db_query($strSQL,$conn);
	}
	
	//	hide colunm headers if needed
	$recordsonpage = $numrows-($mypage-1)*$PageSize;
	if($recordsonpage>$PageSize)
		$recordsonpage = $PageSize;
		
	$xt->assign("page_number",true);
	$xt->assign("maxpages",$maxpages);
	$xt->assign("pageno",$mypage);
}
else
{
	$listarray = false;
	if($eventObj->exists("ListQuery"))
		$listarray=$eventObj->ListQuery($pageObject->searchClauseObj,$_SESSION[$strTableName."_arrFieldForSort"],$_SESSION[$strTableName."_arrHowFieldSort"],$_SESSION[$strTableName."_mastertable"],$masterKeysReq,$selected_recs,$PageSize,$mypage);
	if($listarray!==false)
		$rs = $listarray;
	else
		$rs = db_query($strSQL,$conn);
	$recordsonpage = $numrows;
	$maxpages = ceil($recordsonpage/30);
	$xt->assign("page_number",true);
	$xt->assign("maxpages",$maxpages);
	
}


$fieldsArr = array();
$arr = array();
$arr['fName'] = "full_addr";
$arr['viewFormat'] = ViewFormat("full_addr", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "other_addr";
$arr['viewFormat'] = ViewFormat("other_addr", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "id";
$arr['viewFormat'] = ViewFormat("id", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "company_id";
$arr['viewFormat'] = ViewFormat("company_id", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "address";
$arr['viewFormat'] = ViewFormat("address", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "city_id";
$arr['viewFormat'] = ViewFormat("city_id", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "port_id";
$arr['viewFormat'] = ViewFormat("port_id", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "phone1";
$arr['viewFormat'] = ViewFormat("phone1", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "phone2";
$arr['viewFormat'] = ViewFormat("phone2", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "fax1";
$arr['viewFormat'] = ViewFormat("fax1", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "fax2";
$arr['viewFormat'] = ViewFormat("fax2", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "email";
$arr['viewFormat'] = ViewFormat("email", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "website";
$arr['viewFormat'] = ViewFormat("website", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "primary";
$arr['viewFormat'] = ViewFormat("primary", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "company_name";
$arr['viewFormat'] = ViewFormat("company_name", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "city_name";
$arr['viewFormat'] = ViewFormat("city_name", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "port_name";
$arr['viewFormat'] = ViewFormat("port_name", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "state_name";
$arr['viewFormat'] = ViewFormat("state_name", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "state_code";
$arr['viewFormat'] = ViewFormat("state_code", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "city_code";
$arr['viewFormat'] = ViewFormat("city_code", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "country_code";
$arr['viewFormat'] = ViewFormat("country_code", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "country_name";
$arr['viewFormat'] = ViewFormat("country_name", $strTableName);
$fieldsArr[] = $arr;
$arr = array();
$arr['fName'] = "pin_code";
$arr['viewFormat'] = ViewFormat("pin_code", $strTableName);
$fieldsArr[] = $arr;
$pageObject->setGoogleMapsParams($fieldsArr);

$colsonpage=1;
if($colsonpage>$recordsonpage)
	$colsonpage=$recordsonpage;
if($colsonpage<1)
	$colsonpage=1;


//	fill $rowinfo array
	$pages = array();
	$rowinfo = array();
	$rowinfo["data"]=array();
	if($eventObj->exists("ListFetchArray"))
		$data = $eventObj->ListFetchArray($rs);
	else
		$data = db_fetch_array($rs);	

	while($data)
	{
		if($eventObj->exists("BeforeProcessRowPrint"))
		{
			if(!$eventObj->BeforeProcessRowPrint($data))
			{
				if($eventObj->exists("ListFetchArray"))
					$data = $eventObj->ListFetchArray($rs);
				else
					$data = db_fetch_array($rs);
				continue;
			}
		}
		break;
	}
	
	while($data && ($all || $recno<=$PageSize))
	{
		$row=array();
		$row["grid_record"]=array();
		$row["grid_record"]["data"]=array();
		for($col=1;$data && ($all || $recno<=$PageSize) && $col<=1;$col++)
		{
			$record=array();
			$recno++;
			$records++;
			$keylink="";
			$keylink.="&key1=".htmlspecialchars(rawurlencode(@$data["id"]));


//	full_addr - 
			$value="";
				$value = ProcessLargeText(GetData($data,"full_addr", ""),"field=full%5Faddr".$keylink,"",MODE_PRINT);
			$record["full_addr_value"]=$value;

//	other_addr - 
			$value="";
				$value = ProcessLargeText(GetData($data,"other_addr", ""),"field=other%5Faddr".$keylink,"",MODE_PRINT);
			$record["other_addr_value"]=$value;

//	id - 
			$value="";
				$value = ProcessLargeText(GetData($data,"id", ""),"field=id".$keylink,"",MODE_PRINT);
			$record["id_value"]=$value;

//	company_id - 
			$value="";
				$value=DisplayLookupWizard("company_id",$data["company_id"],$data,$keylink,MODE_PRINT);
			$record["company_id_value"]=$value;

//	address - HTML
			$value="";
				$value = GetData($data,"address", "HTML");
			$record["address_value"]=$value;

//	city_id - 
			$value="";
				$value=DisplayLookupWizard("city_id",$data["city_id"],$data,$keylink,MODE_PRINT);
			$record["city_id_value"]=$value;

//	port_id - 
			$value="";
				$value=DisplayLookupWizard("port_id",$data["port_id"],$data,$keylink,MODE_PRINT);
			$record["port_id_value"]=$value;

//	phone1 - 
			$value="";
				$value = ProcessLargeText(GetData($data,"phone1", ""),"field=phone1".$keylink,"",MODE_PRINT);
			$record["phone1_value"]=$value;

//	phone2 - 
			$value="";
				$value = ProcessLargeText(GetData($data,"phone2", ""),"field=phone2".$keylink,"",MODE_PRINT);
			$record["phone2_value"]=$value;

//	fax1 - 
			$value="";
				$value = ProcessLargeText(GetData($data,"fax1", ""),"field=fax1".$keylink,"",MODE_PRINT);
			$record["fax1_value"]=$value;

//	fax2 - 
			$value="";
				$value = ProcessLargeText(GetData($data,"fax2", ""),"field=fax2".$keylink,"",MODE_PRINT);
			$record["fax2_value"]=$value;

//	email - 
			$value="";
				$value = ProcessLargeText(GetData($data,"email", ""),"field=email".$keylink,"",MODE_PRINT);
			$record["email_value"]=$value;

//	website - 
			$value="";
				$value = ProcessLargeText(GetData($data,"website", ""),"field=website".$keylink,"",MODE_PRINT);
			$record["website_value"]=$value;

//	primary - Checkbox
			$value="";
				$value = GetData($data,"primary", "Checkbox");
			$record["primary_value"]=$value;

//	company_name - 
			$value="";
				$value = ProcessLargeText(GetData($data,"company_name", ""),"field=company%5Fname".$keylink,"",MODE_PRINT);
			$record["company_name_value"]=$value;

//	city_name - 
			$value="";
				$value = ProcessLargeText(GetData($data,"city_name", ""),"field=city%5Fname".$keylink,"",MODE_PRINT);
			$record["city_name_value"]=$value;

//	port_name - 
			$value="";
				$value = ProcessLargeText(GetData($data,"port_name", ""),"field=port%5Fname".$keylink,"",MODE_PRINT);
			$record["port_name_value"]=$value;

//	state_name - 
			$value="";
				$value = ProcessLargeText(GetData($data,"state_name", ""),"field=state%5Fname".$keylink,"",MODE_PRINT);
			$record["state_name_value"]=$value;

//	state_code - 
			$value="";
				$value = ProcessLargeText(GetData($data,"state_code", ""),"field=state%5Fcode".$keylink,"",MODE_PRINT);
			$record["state_code_value"]=$value;

//	city_code - 
			$value="";
				$value = ProcessLargeText(GetData($data,"city_code", ""),"field=city%5Fcode".$keylink,"",MODE_PRINT);
			$record["city_code_value"]=$value;

//	country_code - 
			$value="";
				$value = ProcessLargeText(GetData($data,"country_code", ""),"field=country%5Fcode".$keylink,"",MODE_PRINT);
			$record["country_code_value"]=$value;

//	country_name - 
			$value="";
				$value = ProcessLargeText(GetData($data,"country_name", ""),"field=country%5Fname".$keylink,"",MODE_PRINT);
			$record["country_name_value"]=$value;

//	pin_code - 
			$value="";
				$value = ProcessLargeText(GetData($data,"pin_code", ""),"field=pin%5Fcode".$keylink,"",MODE_PRINT);
			$record["pin_code_value"]=$value;
			if($col<$colsonpage)
				$record["endrecord_block"]=true;
			$record["grid_recordheader"]=true;
			$record["grid_vrecord"]=true;
			
			if($eventObj->exists("BeforeMoveNextPrint"))
				$eventObj->BeforeMoveNextPrint($data,$row,$record);
				
			$row["grid_record"]["data"][]=$record;
			
			if($eventObj->exists("ListFetchArray"))
				$data = $eventObj->ListFetchArray($rs);
			else
				$data = db_fetch_array($rs);
				
			while($data)
			{
				if($eventObj->exists("BeforeProcessRowPrint"))
				{
					if(!$eventObj->BeforeProcessRowPrint($data))
					{
						if($eventObj->exists("ListFetchArray"))
							$data = $eventObj->ListFetchArray($rs);
						else
							$data = db_fetch_array($rs);
						continue;
					}
				}
				break;
			}
		}
		if($col<=$colsonpage)
		{
			$row["grid_record"]["data"][count($row["grid_record"]["data"])-1]["endrecord_block"]=false;
		}
		$row["grid_rowspace"]=true;
		$row["grid_recordspace"] = array("data"=>array());
		for($i=0;$i<$colsonpage*2-1;$i++)
			$row["grid_recordspace"]["data"][]=true;
		
		$rowinfo["data"][]=$row;
		
		if($all && $records>=30)
		{
			$page=array("grid_row" =>$rowinfo);
			$page["pageno"]=$pageindex;
			$pageindex++;
			$pages[] = $page;
			$records=0;
			$rowinfo=array();
		}
		
	}
	if(count($rowinfo))
	{
		$page=array("grid_row" =>$rowinfo);
		if($all)
			$page["pageno"]=$pageindex;
		$pages[] = $page;
	}
	
	for($i=0;$i<count($pages);$i++)
	{
	 	if($i<count($pages)-1)
			$pages[$i]["begin"]="<div name=page class=printpage>";
		else
		    $pages[$i]["begin"]="<div name=page>";
			
		$pages[$i]["end"]="</div>";
	}

	$page=array();
	$page["data"]=&$pages;
	$xt->assignbyref("page",$page);

	

$strSQL=$_SESSION[$strTableName."_sql"];

$isPdfView = true;
if (GetTableData($strTableName, ".isUsebuttonHandlers", false) || $isPdfView || $onLoadJsCode)
{
	$pageObject->body["begin"] .="<script type=\"text/javascript\" src=\"include/jquery.js\"></script>\r\n";
	$pageObject->body["begin"].="<script language=\"JavaScript\" src=\"include/jsfunctions.js\"></script>\r\n";

	if ($pageObject->debugJSMode === true)
	{
		/*$pageObject->body['begin'] .= "<script type=\"text/javascript\" src=\"include/runnerJS/Runner.js\"></script>\r\n".
		"<script type=\"text/javascript\" src=\"include/runnerJS/Util.js\"></script>\r\n".
		"<script type=\"text/javascript\" src=\"include/runnerJS/Observer.js\"></script>\r\n";*/
	$pageObject->body['begin'] .= "<script language=\"JavaScript\" src=\"include/runnerJS/RunnerBase.js\"></script>\r\n";
	}
	else
	{
		$pageObject->body["begin"].="<script language=\"JavaScript\" src=\"include/runnerJS/RunnerBase.js\"></script>\r\n";
	}	
	$pageObject->fillSetCntrlMaps();
	$pageObject->body['end'] .= '<script>';
	$pageObject->body['end'] .= "window.controlsMap = '".jsreplace(my_json_encode($pageObject->controlsHTMLMap))."';";
	$pageObject->body['end'] .= "window.settings = '".jsreplace(my_json_encode($pageObject->jsSettings))."';";
	$pageObject->body['end'] .= '</script>';
	$pageObject->addCommonJs();		
}

$pagename = $_SERVER["REQUEST_URI"];
if(!$pagename)
{
	$pagename=basename(__file__);
	$params="";
	foreach($_GET as $k=>$v)
	{
		if(strlen($params))
			$params.="&";
		$params.=rawurlencode($k)."=".rawurlencode($v);
	}
	if(strlen($params))
		$pagename.="?".$params;
}
if(strpos($pagename,"?")===false)
	$pagename.="?pdf=1";
else
	$pagename.="&pdf=1";


if(!postvalue("pdf"))
{
		$pageObject->AddJSFile("include/pdf");
	$xt->assign("pdflink_block",true);
	$xt->assign("pdflink_attrs","onclick=\"RunPDF(); this.style.display='none'; return false;\"");
	$pageObject->body["end"] .= "<script>
		var page = '".jsreplace($pagename)."';
		</script>
		<iframe src=\"javascript:false;\" name=\"pdf\" class=\"pdf\" style=\"position:absolute;left:-1000px;\">
		</iframe>";
}
$pdf_block = array("begin"=>"<div id=progress>", "end"=>"</div>");
if(count($pages))
	$pages[0]["pdf_block"]=&$pdf_block;

if (GetTableData($strTableName, ".isUsebuttonHandlers", false) || $isPdfView || $onLoadJsCode)
	$pageObject->body["end"] .= "<script>".$pageObject->PrepareJS()."</script>";

$xt->assignbyref("body",$pageObject->body);
$xt->assign("grid_block",true);

$xt->assign("full_addr_fieldheadercolumn",true);
$xt->assign("full_addr_fieldheader",true);
$xt->assign("full_addr_fieldcolumn",true);
$xt->assign("full_addr_fieldfootercolumn",true);
$xt->assign("other_addr_fieldheadercolumn",true);
$xt->assign("other_addr_fieldheader",true);
$xt->assign("other_addr_fieldcolumn",true);
$xt->assign("other_addr_fieldfootercolumn",true);
$xt->assign("id_fieldheadercolumn",true);
$xt->assign("id_fieldheader",true);
$xt->assign("id_fieldcolumn",true);
$xt->assign("id_fieldfootercolumn",true);
$xt->assign("company_id_fieldheadercolumn",true);
$xt->assign("company_id_fieldheader",true);
$xt->assign("company_id_fieldcolumn",true);
$xt->assign("company_id_fieldfootercolumn",true);
$xt->assign("address_fieldheadercolumn",true);
$xt->assign("address_fieldheader",true);
$xt->assign("address_fieldcolumn",true);
$xt->assign("address_fieldfootercolumn",true);
$xt->assign("city_id_fieldheadercolumn",true);
$xt->assign("city_id_fieldheader",true);
$xt->assign("city_id_fieldcolumn",true);
$xt->assign("city_id_fieldfootercolumn",true);
$xt->assign("port_id_fieldheadercolumn",true);
$xt->assign("port_id_fieldheader",true);
$xt->assign("port_id_fieldcolumn",true);
$xt->assign("port_id_fieldfootercolumn",true);
$xt->assign("phone1_fieldheadercolumn",true);
$xt->assign("phone1_fieldheader",true);
$xt->assign("phone1_fieldcolumn",true);
$xt->assign("phone1_fieldfootercolumn",true);
$xt->assign("phone2_fieldheadercolumn",true);
$xt->assign("phone2_fieldheader",true);
$xt->assign("phone2_fieldcolumn",true);
$xt->assign("phone2_fieldfootercolumn",true);
$xt->assign("fax1_fieldheadercolumn",true);
$xt->assign("fax1_fieldheader",true);
$xt->assign("fax1_fieldcolumn",true);
$xt->assign("fax1_fieldfootercolumn",true);
$xt->assign("fax2_fieldheadercolumn",true);
$xt->assign("fax2_fieldheader",true);
$xt->assign("fax2_fieldcolumn",true);
$xt->assign("fax2_fieldfootercolumn",true);
$xt->assign("email_fieldheadercolumn",true);
$xt->assign("email_fieldheader",true);
$xt->assign("email_fieldcolumn",true);
$xt->assign("email_fieldfootercolumn",true);
$xt->assign("website_fieldheadercolumn",true);
$xt->assign("website_fieldheader",true);
$xt->assign("website_fieldcolumn",true);
$xt->assign("website_fieldfootercolumn",true);
$xt->assign("primary_fieldheadercolumn",true);
$xt->assign("primary_fieldheader",true);
$xt->assign("primary_fieldcolumn",true);
$xt->assign("primary_fieldfootercolumn",true);
$xt->assign("company_name_fieldheadercolumn",true);
$xt->assign("company_name_fieldheader",true);
$xt->assign("company_name_fieldcolumn",true);
$xt->assign("company_name_fieldfootercolumn",true);
$xt->assign("city_name_fieldheadercolumn",true);
$xt->assign("city_name_fieldheader",true);
$xt->assign("city_name_fieldcolumn",true);
$xt->assign("city_name_fieldfootercolumn",true);
$xt->assign("port_name_fieldheadercolumn",true);
$xt->assign("port_name_fieldheader",true);
$xt->assign("port_name_fieldcolumn",true);
$xt->assign("port_name_fieldfootercolumn",true);
$xt->assign("state_name_fieldheadercolumn",true);
$xt->assign("state_name_fieldheader",true);
$xt->assign("state_name_fieldcolumn",true);
$xt->assign("state_name_fieldfootercolumn",true);
$xt->assign("state_code_fieldheadercolumn",true);
$xt->assign("state_code_fieldheader",true);
$xt->assign("state_code_fieldcolumn",true);
$xt->assign("state_code_fieldfootercolumn",true);
$xt->assign("city_code_fieldheadercolumn",true);
$xt->assign("city_code_fieldheader",true);
$xt->assign("city_code_fieldcolumn",true);
$xt->assign("city_code_fieldfootercolumn",true);
$xt->assign("country_code_fieldheadercolumn",true);
$xt->assign("country_code_fieldheader",true);
$xt->assign("country_code_fieldcolumn",true);
$xt->assign("country_code_fieldfootercolumn",true);
$xt->assign("country_name_fieldheadercolumn",true);
$xt->assign("country_name_fieldheader",true);
$xt->assign("country_name_fieldcolumn",true);
$xt->assign("country_name_fieldfootercolumn",true);
$xt->assign("pin_code_fieldheadercolumn",true);
$xt->assign("pin_code_fieldheader",true);
$xt->assign("pin_code_fieldcolumn",true);
$xt->assign("pin_code_fieldfootercolumn",true);

	$record_header=array("data"=>array());
	for($i=0;$i<$colsonpage;$i++)
	{
		$rheader=array();
		if($i<$colsonpage-1)
		{
			$rheader["endrecordheader_block"]=true;
		}
		$record_header["data"][]=$rheader;
	}
	$xt->assignbyref("record_header",$record_header);
	$xt->assign("grid_header",true);
	$xt->assign("grid_footer",true);


$templatefile = "company_address_print.htm";
	
if($eventObj->exists("BeforeShowPrint"))
	$eventObj->BeforeShowPrint($xt,$templatefile);

if(!postvalue("pdf"))
	$xt->display($templatefile);
else
{
	$xt->load_template($templatefile);
	$page = $xt->fetch_loaded();
	$pagewidth=postvalue("width")*1.05;
	$pageheight=postvalue("height")*1.05;
	$landscape=false;
	if(postvalue("all"))
	{
		if($pagewidth>$pageheight)
		{
			$landscape=true;
			if($pagewidth/$pageheight<297/210)
				$pagewidth = 297/210*$pageheight;
		}
		else
		{
			if($pagewidth/$pageheight<210/297)
				$pagewidth = 210/297*$pageheight;
		}
	}
	include("plugins/page2pdf.php");
}

?>
